import React, { Component } from 'react';
class Clock extends React.Component {
    constructor(props) {
        super(props);
        this.state = {date: new Date()};
    }

    componentDidMount() {
        this.timerID = setInterval(
            () => this.tick(),
            1000
        );
    }

    componentWillUnmount() {
        clearInterval(this.timerID);
    }

    tick() {
        this.setState({
            date: new Date()
        });
    }
    getFmtValue(value){
        return value >= 10 ? value : "0" + value;
    }
    getFmtDate(){
        let date = this.state.date;
        let minutes =  this.getFmtValue(date.getMinutes());
        let month = this.getFmtValue(date.getMonth() + 1 );
        let day = this.getFmtValue(date.getDate());
        let hours = this.getFmtValue(date.getHours());
        return date.getFullYear() + "-" +
            month + "-" + day + " " +  hours + ":" + minutes;
    }
    render() {
        return (
            <div>
                <span>系统时间: {this.getFmtDate()}</span>
            </div>
        );
    }
}

export  default Clock;